package com.ruoyi.bole.integral.mapper;

import java.util.List;


import com.ruoyi.bole.integral.domain.BoleIntegralOrganization;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 组织关系Mapper接口
 * 
 * @author wujiale
 * @date 2025-07-04
 */
public interface BoleIntegralOrganizationMapper
{
    @Select("SELECT " +
            "org_id AS orgId, " +
            "org_name AS orgName, " +
            "parent_id AS parentId, " +
            "level AS level, " +
            "points AS points, " +
            "allocated_integral AS allocatedIntegral, " +
            "status AS status, " +
            "user_id AS userId " +
            "FROM bole_integral_organization " +
            "WHERE user_id = #{userId} and status='0'")
    public BoleIntegralOrganization getByUserId(Long userId);

    @Select("SELECT " +
            "org_id AS orgId, " +
            "org_name AS orgName, " +
            "parent_id AS parentId, " +
            "level AS level, " +
            "points AS points, " +
            "allocated_integral AS allocatedIntegral, " +
            "status AS status, " +
            "user_id AS userId " +
            "FROM bole_integral_organization " +
            "WHERE parent_id = #{parentId} and status='0'")
    public List<BoleIntegralOrganization> getListByParentId(Long parentId);
    @Select("SELECT " +
            "org_id AS orgId, " +
            "org_name AS orgName, " +
            "parent_id AS parentId, " +
            "level AS level, " +
            "points AS points, " +
            "allocated_integral AS allocatedIntegral, " +
            "status AS status, " +
            "user_id AS userId " +
            "FROM bole_integral_organization " +
            "WHERE parent_id = #{parentId} and status='0' and level=2")
    public List<BoleIntegralOrganization> getListAndIsGroupByParentId(Long parentId);

    //分配可分配积分
    @Update("UPDATE bole_integral_organization " +
            "SET points = points - #{reducePoints} " +  // 核心：积分 = 当前积分 - 扣除值
            "WHERE org_id = #{orgId} " +
            "AND points >= #{reducePoints}")  // 防止积分扣成负数
    int reducePointsByoOgId(
            @Param("orgId") Long orgId,
            @Param("reducePoints") Long reducePoints
    );
    @Update("UPDATE bole_integral_organization " +
            "SET points = points + #{reducePoints} " +  // 核心：积分 = 当前积分 - 扣除值
            "WHERE org_id = #{orgId} " )  // 防止积分扣成负数
    int addPointsByOrgId(
            @Param("orgId") Long orgId,
            @Param("reducePoints") Long reducePoints
    );

    //分配到了可以使用的积分
    @Update("UPDATE bole_integral_organization " +
            "SET allocated_integral = allocated_integral + #{allocatedIntegral} " +  // 核心：积分 = 当前积分 - 扣除值
            "WHERE org_id = #{orgId} " )  // 防止积分扣成负数
    int addAllocatedIntegralByOrgId(
            @Param("orgId") Long orgId,
            @Param("allocatedIntegral") Long allocatedIntegral
    );
//    @Insert()
    /**
     * 查询组织关系
     * 
     * @param orgId 组织关系主键
     * @return 组织关系
     */
    public BoleIntegralOrganization selectBoleIntegralOrganizationByOrgId(Long orgId);

    /**
     * 查询组织关系列表
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 组织关系集合
     */
    public List<BoleIntegralOrganization> selectBoleIntegralOrganizationList(BoleIntegralOrganization boleIntegralOrganization);

    /**
     * 新增组织关系
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 结果
     */
    public int insertBoleIntegralOrganization(BoleIntegralOrganization boleIntegralOrganization);

    /**
     * 修改组织关系
     * 
     * @param boleIntegralOrganization 组织关系
     * @return 结果
     */
    public int updateBoleIntegralOrganization(BoleIntegralOrganization boleIntegralOrganization);

    /**
     * 删除组织关系
     * 
     * @param orgId 组织关系主键
     * @return 结果
     */
    public int deleteBoleIntegralOrganizationByOrgId(Long orgId);

    /**
     * 批量删除组织关系
     * 
     * @param orgIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteBoleIntegralOrganizationByOrgIds(Long[] orgIds);
}
